<?php
/**
 * Regist.php
 *
 * 用户注册表单生成类
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Form
 * @author 		Guya
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/**
 * 用户注册表单生成类.
 *
 * @category	Momoger
 * @package 	Momoger_Form
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Form_Regist {

	/**
	 * 创建注册FORM, 设置3个元素, 帐号, 密码 和 邮件地址
	 * 登录名只能是字母和数字组合, 无大小写区别.
	 * 登录密码可以是任意字符组合。
	 * 帐号和密码长度限制在 4 -20 个字符内.
	 *
	 * @param string $form_action 表单提交地址
	 * @return Zend_Form
	 */
	public function getRegistForm($form_action = null) {

		//the input elements attribute: name. There is need validate
		$inputs = array( 'account' => 'account', 'passwd' => 'passwd', 'email' => 'email');

		//create form object
		$form = new Zend_Form();
		$form->setAction($form_action)
			 ->setMethod('post');

		//create user account input element
		$userAccount = $form->createElement('text', $inputs['account'], array('label' => 'Regist Login Account:', 'class' => 'textInput'));

		//add validator and filter for account input
		$userAccount->addValidator('Alnum')
					->addValidator('Regex', FALSE, array('/^[a-z0-9]+/'))
					->addValidator('StringLength', FALSE, array(4,20))
					->setRequired(TRUE)
					->addFilter('StringToLower');

		//create user password input element
		$userPasswd = $form->createElement('password', $inputs['passwd'], array('label' => 'Regist Login Password:', 'class' => 'textInput'));

		//add validator and filter for passwd input
		$userPasswd->addValidator('StringLength', FALSE, array(4,20))
				   ->setRequired(TRUE)
				   ->addFilter('StringToLower');

		//create user password input element
		$userEmail = $form->createElement('text', $inputs['email'], array('label' => 'Your E-mail Address:', 'class' => 'textInput'));

		//add validator and filter for passwd input
		$userEmail->addValidator('EmailAddress')
				   ->setRequired(TRUE);

		$submit = $form->createElement('submit', 'submit', array('label' => 'Ok! Give me a account!'));
		$form->addElement($userAccount)
			 ->addElement($userPasswd)
			 ->addElement($userEmail)
			 ->addElement($submit);
		return $form;
	}

	/**
	 * 获取自定义错误信息,注册信息失败可以获取相关的失败信息.
	 *
	 * @param array $errors Zend_Form 错误提示信息
	 * @return array
	 */
	public function getErrorMsgs(array $errors) {

		/** Momoger_Form_Message */
		//require_once 'Message.php';

		$labels = array(
			'account' => 'Your account invalid: ',
			'passwd' => 'Your password invalid: ',
			'email' => 'Your E-Mail address invalid: '
		);

		return Momoger_Form_Message::getExceptionMsgs($errors, $labels);
	}
}